﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace LogHelper
{
    public class LogHandler
    {
        string logFileName;
        FileStream fs = null;
        StreamWriter sw = null;
        public LogHandler(string path)
        {
            logFileName = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            fs = new FileStream(logFileName, FileMode.Append, FileAccess.Write);;
            sw = new StreamWriter(fs);
            //sw.AutoFlush = true;
        }

        public void WriteLine(string strLog, LogLevel logLevel=LogLevel.Notice, bool isDisplay=true)
        {
            string str = $"[{logLevel}] {DateTime.Now.ToString("yy-MM-dd HH:mm:ss fff")}  {strLog}";
            if (isDisplay)
            {
                Console.WriteLine(str);
            }
            sw.WriteLine(str);
            sw.Flush();
        }

        public void Dispose()
        {
            sw.Close();
            fs.Close();
        }
    }


    public enum LogLevel
    {
        Debug=0,
        Notice,
        Warning,
        Error,
    }
}
